import { useCallback, useEffect, useState } from 'react';
import { BigNumber } from 'ethers';
import ERC20 from '../basis-cash/ERC20';
import useBasisCash from './useBasisCash';
import config from '../config';

const useLPBonusRatio = () => {
  const [ratio, setRatio] = useState(0);
  const basisCash = useBasisCash();

  const fetchRatio = useCallback(async () => {
    setRatio(await basisCash.getLPBonusRatio());
  }, [basisCash?.isUnlocked, config.group]);

  useEffect(() => {
    if (basisCash?.isUnlocked) {
      fetchRatio().catch((err) =>
        console.warn(`Failed to fetch token balance: ${err.stack}`),
      );
      let refreshInterval = setInterval(fetchRatio, config.refreshInterval);
      return () => clearInterval(refreshInterval);
    }
  }, [basisCash?.isUnlocked, config.group]);

  return ratio;
};

export default useLPBonusRatio;
